import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { NzFormatEmitEvent } from 'ng-zorro-antd';
import {AreaService} from "../../../routes/sys-setting/area-manage/area.service";

@Component({
  selector: 'app-left-tree',
  templateUrl: './left-tree.component.html',
  styleUrls: ['./left-tree.component.less']
})
export class LeftTreeComponent implements OnInit {
  @Input()nodes: any;
  @Input()maxLevel: number
  @Output()selectChange: EventEmitter<any> = new EventEmitter<any>();
  searchValue = '';
  nzEvent(event: NzFormatEmitEvent): void {
    // load child async
    if (event.eventName === 'expand') {
      const node = event.node;
      if (node && node.getChildren().length === 0 && node.isExpanded) {
        console.log(event)
        this.loadNode(event.node).then(data => {
          console.log(node.level)
          if(node.level == this.maxLevel) {
            data.forEach( item => {
              item.isLeaf = true;
            })
          }
          node.addChildren(data);
        }, () => {
          node.addChildren([])
        });
      }
    }
  }
  loadNode(data): Promise <any>{
    return this.area.getAreaTreeNode(data.key)
  }
  _selectChange(event) {
    this.selectChange.emit(event);
  }
  constructor(
    private area: AreaService
  ) { }

  ngOnInit() {
    this.area.getAreaTreeNode(0).then(res => {
      this.nodes = res;
      console.log(this.nodes)
    })
  }

}
